﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TunnelManager.Tools;

namespace TunnelManager.UserManager
{
    public partial class UserManagment : Form
    {
        public UserManagment()
        {
            InitializeComponent();
        }

        private string editStatus = "";

        private void UserManagment_Load(object sender, EventArgs e)
        {
            dataGridView1.Size = new Size(this.ClientSize.Width, dataGridView1.Size.Height);
            groupBox1.Size = new Size(this.ClientSize.Width, groupBox1.Size.Height);

            fillTheGrid();
            SqlDBHelper sdh = new SqlDBHelper();
            string sql2 = "SELECT [RoleID],[Name],[Role] FROM [TunnelXAT1].[dbo].[M_Role]";
            DataTable dt2 = sdh.GetTable(sql2);
            cbRole.ValueMember = "RoleID";
            cbRole.DisplayMember = "Name";
            cbRole.DataSource = dt2;

            ArrayList list = new ArrayList();
            list.Add(new DictionaryEntry("0", "否"));
            list.Add(new DictionaryEntry("1", "是"));

            cbIsWorking.DisplayMember = "Value";
            cbIsWorking.ValueMember = "Key";
            cbIsWorking.DataSource = list;
        }
        private void fillTheGrid()
        {
            SqlDBHelper sdh = new SqlDBHelper();
            string sql = "SELECT M_User.UserID, M_User.Password, M_User.Name, M_User.RoleID, M_Role.Name AS RoleName, CASE WHEN M_User.isWorking=0 THEN '否' WHEN M_User.isWorking=1 THEN '是' END AS isWorking FROM M_User LEFT OUTER JOIN M_Role ON M_User.RoleID = M_Role.RoleID WHERE (M_User.UserID <> 'admin')";
            DataTable dt = sdh.GetTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void UserManagment_Resize(object sender, EventArgs e)
        {
            dataGridView1.Size = new Size(this.ClientSize.Width, dataGridView1.Size.Height);
            groupBox1.Size = new Size(this.ClientSize.Width, groupBox1.Size.Height);
        }



        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            tbUserID.Text = dataGridView1.CurrentRow.Cells["UserID"].Value.ToString();
            tbPassword.Text = dataGridView1.CurrentRow.Cells["Password"].Value.ToString();
            tbName.Text = dataGridView1.CurrentRow.Cells["PeopleName"].Value.ToString();
            cbRole.Text = dataGridView1.CurrentRow.Cells["RoleName"].Value.ToString();
            cbIsWorking.Text = dataGridView1.CurrentRow.Cells["isWorking"].Value.ToString();
            DisableEditors();
        }

        private void EnableEditors()
        {
            tbUserID.Enabled = false;
            tbPassword.Enabled = true;
            tbName.Enabled = true;
            cbRole.Enabled = true;
            cbIsWorking.Enabled = true;
            btOK.Enabled = true;
        }
        private void DisableEditors()
        {
            tbUserID.Enabled = false;
            tbPassword.Enabled = false;
            tbName.Enabled = false;
            cbRole.Enabled = false;
            cbIsWorking.Enabled = false;
            btOK.Enabled = false;
        }
        private void ClearEditors()
        {
            tbUserID.Enabled = true;
            tbPassword.Enabled = true;
            tbName.Enabled = true;
            cbRole.Enabled = true;
            cbIsWorking.Enabled = true;

            tbUserID.Text = "";
            tbPassword.Text = "";
            tbName.Text = "";
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            editStatus = "编辑";
            EnableEditors();
            tbUserID.Text = dataGridView1.CurrentRow.Cells["UserID"].Value.ToString();
            tbPassword.Text = dataGridView1.CurrentRow.Cells["Password"].Value.ToString();
            tbName.Text = dataGridView1.CurrentRow.Cells["PeopleName"].Value.ToString();
            cbRole.Text = dataGridView1.CurrentRow.Cells["RoleName"].Value.ToString();
            cbIsWorking.Text = dataGridView1.CurrentRow.Cells["isWorking"].Value.ToString();
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            editStatus = "新增";
            btOK.Enabled = true;
            ClearEditors();
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            if ((string.IsNullOrEmpty(tbName.Text)) || (string.IsNullOrEmpty(tbPassword.Text) || (string.IsNullOrEmpty(tbName.Text))))
            {
                MessageBox.Show("请填写完整信息");
                return;
            }
            
            switch (editStatus)
            {
                case "新增":
                    {
                        addNewUser();
                    }
                    break;
                case "编辑":
                    {
                        editUser();
                    }
                    break;
                default:
                    {
                        MessageBox.Show("非法操作");
                        btOK.Enabled = false;
                        DisableEditors();
                    }
                    break;
            }
        }

        private void addNewUser()
        {
            if (!IDCardNumber.CheckIDCard(tbUserID.Text))
            {
                MessageBox.Show("请输入正确的身份证号码");
                return;
            }

            SqlDBHelper sdh = new SqlDBHelper();
            string idNumIsExist = "select UserID from M_User where UserID='" + tbUserID.Text + "'";

            DataTable idNum = sdh.GetTable(idNumIsExist);
            if (idNum.Rows.Count != 0)
            {
                MessageBox.Show("已存在当前身份证号码");
                return;
            }

            string addNewUser = "INSERT INTO [M_User] ( [UserID] ,[Password] ,[Name] ,[RoleID] ,[isWorking] ) VALUES ('" + tbUserID.Text + "','" + tbPassword.Text + "','" + tbName.Text + "','" + cbRole.SelectedValue.ToString() + "','" + cbIsWorking.SelectedValue.ToString() + "')";
            int i = sdh.ExecuteCommand(addNewUser);
            if (i == 1)
            {
                MessageBox.Show("人员添加成功");
                btOK.Enabled = false;
                DisableEditors();
                fillTheGrid();
            }
            else
            {
                MessageBox.Show("人员添加失败，请重试");
            }

        }
        private void editUser()
        {
            SqlDBHelper sdh = new SqlDBHelper();
            string editUser = "update [M_User] set [Password] = '" + tbPassword.Text + "',[Name] = '" + tbName.Text + "',[RoleID] = '" + cbRole.SelectedValue.ToString() + "',[isWorking] = '" + cbIsWorking.SelectedValue.ToString() + "' where [UserID]='" + tbUserID.Text + "'";
            int i = sdh.ExecuteCommand(editUser);
            if (i == 1)
            {
                MessageBox.Show("人员编辑成功");
                btOK.Enabled = false;
                DisableEditors();
                fillTheGrid();
            }
            else
            {
                MessageBox.Show("人员编辑失败，请重试");
            }
        }
    }
}
